package com.vegetable.modules.controller.inbound;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.inbound.PurchaseDeliveryReturn;
import com.vegetable.modules.entity.inbound.po.PurchaseDeliveryReturnPO;
import com.vegetable.modules.service.inbound.IPurchaseDeliveryReturnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 入库-采购收货退货 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-09
 */
@Slf4j
@RestController
@RequestMapping("/inStorage/purchaseDeliveryReturn")
@AllArgsConstructor
@Api(value = "入库采购收货退货", tags = "入库采购收货退货")
public class PurchaseDeliveryReturnController extends AbstractController {

    private IPurchaseDeliveryReturnService purchaseDeliveryReturnService;

    /**
     * 分页查询采购退货记录
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询采购退货记录", notes = "分页查询采购退货记录")
    public R list(PurchaseDeliveryReturnPO po) {
        log.info("分页查询采购退货记录: {}", JSON.toJSONString(po));
        return purchaseDeliveryReturnService.queryPage(po);
    }

    /**
     * 根据订单ID查询退货信息
     */
    @GetMapping("/getReturnsByDeliveryId")
    @ApiOperation(value = "根据订单ID查询退货信息", notes = "根据订单ID查询退货信息")
    public R getReturnsByDeliveryId(Long deliveryId) {
        if (deliveryId == null) {
            return R.fail("订单ID不能为空");
        }
        log.info("根据订单ID查询退货信息: {}", deliveryId);
        return R.data(purchaseDeliveryReturnService.getReturnsByDeliveryId(deliveryId));
    }

    /**
     * 根据ID获取退货详情
     */
    @GetMapping("/getReturnDetail")
    @ApiOperation(value = "根据ID获取退货详情", notes = "根据ID获取退货详情")
    public R getReturnDetail(Long id) {
        if (id == null) {
            return R.fail("退货ID不能为空");
        }
        log.info("根据ID获取退货详情: {}", id);
        return purchaseDeliveryReturnService.getReturnDetail(id);
    }

    /**
     * 保存采购退货信息
     */
    @SysLog(value = "保存采购退货信息")
    @PostMapping("/saveReturn")
    @ApiOperation(value = "保存采购退货信息", notes = "保存采购退货信息")
    public R saveReturn(@RequestBody PurchaseDeliveryReturn purchaseDeliveryReturn) {
        log.info("保存采购退货信息: {}", JSON.toJSONString(purchaseDeliveryReturn));
        try {
            purchaseDeliveryReturn.setCreateUserId(getUserId());
            return purchaseDeliveryReturnService.saveReturn(purchaseDeliveryReturn);
        } catch (Exception e) {
            log.error("保存采购退货信息失败", e);
            return R.fail("保存失败: " + e.getMessage());
        }
    }

    /**
     * 删除采购退货信息
     */
    @SysLog(value = "删除采购退货信息")
    @GetMapping("/deleteReturn")
    @ApiOperation(value = "删除采购退货信息", notes = "删除采购退货信息")
    public R deleteReturn(Long id) {
        log.info("删除采购退货信息: {}", id);
        try {
            if (id == null) {
                return R.fail("退货ID不能为空");
            }
            // 删除主表记录
            purchaseDeliveryReturnService.removeById(id);
            // 删除明细记录
            purchaseDeliveryReturnService.getBaseMapper().delete(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<com.vegetable.modules.entity.inbound.PurchaseDeliveryReturn>()
                    .eq("return_id", id)
            );
            return R.success("删除成功");
        } catch (Exception e) {
            log.error("删除采购退货信息失败", e);
            return R.fail("删除失败: " + e.getMessage());
        }
    }
}